package com.self.smallma.myblogweb.controller;

import java.util.Arrays;
import java.util.Map;

import com.self.smallma.myblogweb.common.utils.PageUtils;
import com.self.smallma.myblogweb.common.utils.R;
import com.self.smallma.myblogweb.vo.FirstPageBlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.self.smallma.myblogweb.entity.BlogEntity;
import com.self.smallma.myblogweb.service.BlogService;




/**
 * 
 *
 * @author mahuanyu
 * @email 1914096395@qq.com
 * @date 2021-07-17 17:48:05
 */
@RestController
@RequestMapping("myblogweb/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("myblogweb:blog:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = blogService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{blogId}")
    //@RequiresPermissions("myblogweb:blog:info")
    public R info(@PathVariable("blogId") Long blogId){
		BlogEntity blog = blogService.getById(blogId);

        return R.ok().put("blog", blog);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("myblogweb:blog:save")
    public R save(@RequestBody FirstPageBlogVo blog){
//		blogService.save(blog);
        System.out.println(blog);
        //保存提交的数据（与实体类不一样）
      BlogEntity blogEntity =  blogService.saveWithCustom(blog);
      if (blogEntity!=null){
          return R.ok();
      }else{
          return R.error();
      }


    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("myblogweb:blog:update")
    public R update(@RequestBody FirstPageBlogVo blog){

		//blogService.updateById(blog);
        BlogEntity blogEntity =   blogService.updateBatchByIdWithUpdateTime(blog);

        return R.ok();
    }

    @RequestMapping("/updatebutton")
    //@RequiresPermissions("myblogweb:blog:update")
    public R updateButton(@RequestBody BlogEntity blog){
        blogService.updateById(blog);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("myblogweb:blog:delete")
    public R delete(@RequestBody Long[] blogIds){
		blogService.removeByIds(Arrays.asList(blogIds));

        return R.ok();
    }

}
